package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaCilent;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: xrl
 * @Date: 2022/10/22  16:16
 * @Version 1.0
 * 用户认证
 */
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    /**
     * 查询列表
     */
    @Override
    public ResponseResult getList(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //检查参数
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();

        //状态
        if (dto.getStatus() != null) {
            wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        //调用方法查询
        page = page(page, wrapper);

        //返回结果
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        //为result 设置分页查询数据
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 审核失败
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "id不存在");
        }
        //代表id存在
        LambdaQueryWrapper<ApUserRealname> wrapper = Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getUserId, dto.getId());
        ApUserRealname realname = getById(dto.getId());
        realname.setReason(dto.getMsg());
        //更新
        updateById(realname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private IWemediaCilent wemediaCilent;

    @Autowired
    private ApUserService apUserService;

    /**
     * 审核成功
     * 用户通过审核后需要开通  自媒体账号 （该账号的用户名和密码与app一致）
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "id不存在");
        }
        //修改审核状态为9
        ApUserRealname realname = getById(dto.getId());
        realname.setStatus((short) 9);
        //更新
        updateById(realname);

        //根据id查询app_user
        LambdaQueryWrapper<ApUser> wrapper = Wrappers.<ApUser>lambdaQuery()
                .eq(ApUser::getId, realname.getUserId());
        ApUser user = apUserService.getOne(wrapper);

        //查询申请审核用户的账户名以及密码
        WmUser wmUser = new WmUser();
        wmUser.setName(user.getName());
        wmUser.setPassword(user.getPassword());

        //开通自媒体账号
        wemediaCilent.ok(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
